def power1(x):
    return x*x
print(power1(5))


def power2(x, n):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s
print(power2(5, 2))
print(power2(5, 3))


# 函数的默认参数
def power3(x, n=2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s
print(power3(5))
print(power3(5, 2))


# 可变参数
def calc(numbers):
    sum = 0
    for n in numbers:
        sum = sum + n*n
    return sum
print(calc([1, 2, 3]))
print(calc([2, 3, 4, 5, 6]))


def calc1(*numbers):
    result = 0
    for n in numbers:
        result += n*n
    return result
print(calc1(1, 2, 3))
print(calc1(2, 3, 4, 5, 6))


# 递归函数
def fact(n):
    if n == 1:
        return 1
    return n*fact(n-1)
print(fact(1))
print(fact(5))


def fact1(n):
    return fact_iter(n, 1)


def fact_iter(num, prodect):
    if num == 1:
        return prodect
    return fact_iter(num-1, num*prodect)
print(fact(5))


# 汉诺塔练习
def do(start, end):
    print(start + ' ==> ' + end)


def move(n, a, b, c):
    if n == 1:
        print(do(a, c))
    else:
        move(n-1, a, c, b)
        do(a, c)
        move(n-1, b, a, c)
print(move(3, 'A', 'B', 'C'))


import os

print([d for d in os.listdir('.')])


def f(n):
    return n*n
L = list(range(1, 10))
r = map(f, L)
print(list(r))


result = map(lambda n: n*n, list(range(1, 10)))
print('lambda匿名函数结果:', list(result))


print([x*x for x in range(1, 10)])
print([x*x for x in range(1, 10) if x % 2 != 0])
print([x for x in range(1, 10) if x % 2 != 0])
print([x for x in range(1, 10) if x % 2 == 0])


from functools import reduce


def fn(x, y):
    return x * 10 + y


print(reduce(fn, [n for n in range(1, 10)if n % 2 != 0]))
